/*
 * @Author: 橘子
 * @Date: 2023-03-10 16:07
 * @Project_description: 格式化侧边栏路由
 * @Description: 代码是我抄的，不会也是真的
 */

'use strict'
import { uniqueId } from 'lodash'

//
/**
 * 格式化菜单栏
 * @param menu{[]} 菜单tree
 * @returns {*[]}
 */
function supplementPath(menu) {
  return menu.reduce((router, e) => {
    let onRouter = {}
    if (e.children && e.children.length === 1) {
      onRouter = {
        ...e.children[0].meta,
        path: e.children[0].path || uniqueId('d2-menu-empty-'),
      }
    } else if (e.children && e.children.length > 1) {
      onRouter = {
        name: e.name,
        path: e.path || uniqueId('d2-menu-empty-'),
        ...e.meta,
        children: supplementPath(e.children),
      }
    } else {
      onRouter = {
        ...e,
        ...e.meta,
      }
    }
    if (Object.keys(onRouter).length > 0) {
      router.push(onRouter)
    }
    return router
  }, [])
}

//格式化菜单栏
export const supPath = supplementPath
